drawPath() is used to draw series of strokes.
Syntax
import androidx.compose.foundation.Canvas
import androidx.compose.ui.graphics.Path
import androidx.compose.ui.graphics.SolidColor
Canvas(Modifier.fillMaxSize()) {
drawPath(
path = Path().apply {
moveTo(x = 100f, y = 100f) //Move starting point
lineTo(x = 400f, y = 100f) //Draw line from previous point to this point
relativeLineTo(dx = 0f, dy = 300f) //Draw line by moving dx and dy from previous point point
},
brush = SolidColor(Color.Red)
)
}
In this example we use Canvas to draw Rectangle.
MainActivity.kt
package com.example.testcompose
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.compose.foundation.Canvas
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.*
import androidx.compose.ui.platform.setContent
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
Canvas(Modifier.fillMaxSize()) {
drawPath(
path = Path().apply {
moveTo(x = 100f, y = 100f)
relativeLineTo(dx = 300f, dy = 0f)
relativeLineTo(dx = 0f, dy = 300f)
moveTo(x = 500f, y = 100f)
lineTo(x = 800f, y = 100f)
lineTo(x = 800f, y = 400f)
},
brush = SolidColor(Color.Red)
)
}
}
}
}